﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SilkeyERP.Model.Repository
{
    public class AddressRepository : IAddressRepository
    {
        private bool disposed = false;
        private Model.SilkeyERPEntities context;

        public AddressRepository()
        {
            if (this.context == null)
                this.context = new SilkeyERPEntities();
        }

        public IEnumerable<Address> GetAll()
        {
            return context.Address.ToList();
        }

        public Address GetByID(int ID)
        {
            return context.Address.Find(ID);
        }

        public void Insert(Address Obj)
        {
            context.Address.Add(Obj);
            Save();
        }

        public void Delete(int ID)
        {
            Address _Address = context.Address.Find(ID);
            context.Address.Remove(_Address);
            Save();
        }

        public void DeleteLogical(int ID)
        {
            Address Obj = GetByID(ID);
            Obj.Enable = false;
            Update(Obj);
        }

        public void Update(Address Obj)
        {
            context.Entry(Obj).State = EntityState.Modified;
            Save();
        }

        public void Save()
        {
            context.SaveChanges();
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
                if (disposing)
                    context.Dispose();
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
